window.qisiren = function (gameHost) {
    let get = function (path, fn) {
        if (path.indexOf("?") > 0) {
            path += "&" + Math.random();
        } else {
            path += "?" + Math.random();
        }
        let xhr = new XMLHttpRequest();
        xhr.open('GET', gameHost + "/game" + path);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200 || xhr.status === 304) {
                if (fn !== undefined) {
                    fn(JSON.parse(xhr.responseText));
                }
            }
        };
        xhr.send();
    }
    let post = function (path, data, fn) {
        let xhr = new XMLHttpRequest();
        xhr.open("POST", gameHost + "/game" + path);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {
                if (fn !== undefined) {
                    fn(JSON.parse(xhr.responseText));
                }
            }
        };
        xhr.send(data);
    };
    let self = this;
    let playerId;
    let gameId;

    setInterval(function () {
        if (!playerId) {
            return;
        }
        get("/watch?pid=" + playerId, function (rs) {
            if (rs.success === false) {
                console.error(rs.msg);
                return;
            }
            for (let i = 0; i < rs.data.length; i++) {
                let n = JSON.parse(rs.data[i]);
                let d = n.data;
                switch (n.type) {
                    case "add":
                        self.onAdd(d.pid, d.name, d.ready);
                        break
                    case "leave":
                        self.onLeave(d);
                        break;
                    case "ready":
                        self.onReady(d.pid, d.ready);
                        break
                    case "index":
                        self.onIndex(d);
                        break
                    case "new":
                        self.onXinpai(d);
                        break
                    case "count":
                        for (let i = 0; i < d.length; i++) {
                            self.onPaishu(d[i].pid, d[i].size);
                        }
                        break;
                    case "active":
                        self.onActive(d.pid, d.isTake);
                        break;
                    case "post":
                        self.onChupai(d.pid, d.pai);
                        break;
                    case "pass":
                        self.onPass(d);
                        break;
                    case "reload":
                        self.onReload(d);
                        break;
                    case "gamestart":
                        self.onGameStart();
                        break;
                    case "gameover":
                        self.onGameOver(d);
                        break
                    case "voice":
                        self.onVoice(d.pid, d.vid);
                        break
                }
            }
        });
    }, 500);

    /**
     * 收到语音
     */
    this.onVoice = function (pid, vid) {

    }

    /**
     * 收到游戏结束
     */
    this.onGameOver = function (data) {
    }

    /**
     * 收到游戏开始
     */
    this.onGameStart = function () {
    }

    /**
     * 收到要求刷新手牌
     */
    this.onReload = function (pais) {
    };

    /**
     * 收到要不起
     */
    this.onPass = function (pid) {
    }

    /**
     * 收到出牌内容
     */
    this.onChupai = function (pid, pais) {
    }

    /**
     * 收到出牌通知
     */
    this.onActive = function (pid, isTake) {
    }

    /**
     * 收到手牌数量
     */
    this.onPaishu = function (pid, count) {
    }

    /**
     * 收到一张新牌
     */
    this.onXinpai = function (pai) {
    }
    /**
     *  收到出牌顺序
     */
    this.onIndex = function (list) {
    }

    /**
     * 收到玩家准备状态变更
     */
    this.onReady = function (pid, isReady) {
    }

    /**
     * 收到玩家离开
     */
    this.onLeave = function (pid) {
    };

    /**
     * 收到新玩家加入
     */
    this.onAdd = function (pid, name, isReady) {
    };

    /**
     * 设置昵称
     */
    this.setNickname = function (name, fn) {
        post("/change", "pid=" + playerId + "&name=" + name, fn);
    }

    /**
     * 登录
     */
    this.login = function (pid, name, fn) {
        post("/login", "id=" + pid + "&name=" + name, function (data) {
            if (data.success) {
                playerId = pid;
            }
            fn(data);
        })
    }

    /**
     * 创建新房间
     */
    this.newGame = function (fn) {
        post("/newgame", "pid=" + playerId, fn);
    }

    /**
     * 游戏房间列表
     * pc=房间内的人数，started=是否已开始
     */
    this.gamelist = function (fn) {
        get("/list", fn);
    };

    /**
     * 加入房间
     */
    this.add = function (gid, fn) {
        post("/add", "gid=" + gid + "&pid=" + playerId, function (rs) {
            if (rs.success) {
                gameId = gid;
            }
            fn(rs);
        });
    };

    /**
     * 离开房间
     */
    this.leave = function (fn) {
        post("/leave", "pid=" + playerId, function () {
            gameId = null;
            fn();
        });
    };

    /**
     * 设置当前准备状态
     */
    this.ready = function (isready, fn) {
        post("/ready", "pid=" + playerId + "&isready=" + (isready ? 1 : 0), fn)
    }

    /**
     * 出牌
     */
    this.chupai = function (pais, wang, fn) {
        post("/chupai", "pid=" + playerId + "&pai=" + pais + "&wang=" + wang, fn)
    }

    /**
     * 要不起
     */
    this.pass = function (fn) {
        post("/pass", "pid=" + playerId, fn);
    }

    /**
     * 发送语音
     */
    this.voice = function (vid, fn) {
        post("/voice", "pid=" + playerId + "&vid=" + vid, fn);
    }
};